package com.bw.module_main

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

class NavTabItem:BaseTabItem{
    private lateinit var mIcon: ImageView
    private lateinit var mTitle: TextView
    private lateinit var mMsgView: RoundMessageView

    private lateinit var mDefaultIcon:Drawable
    private lateinit var mCheckedIcon:Drawable
    private lateinit var mTabTitle:String
    private var mChecked  = false
    private var mMsgNum = 0

    private var mDefaultTextColor = "#4B5563"
    private var mCheckedTextColor = "#FF2442"

    constructor(context: Context):super(context){
        initView(context)
    }

    constructor(context: Context,attributeSet: AttributeSet?):super(context,attributeSet){
        initView(context)
    }

    constructor(context: Context,attributeSet: AttributeSet?,defStyleAttr:Int):super(context,attributeSet,defStyleAttr){
        initView(context)
    }

    private fun initView(context: Context){
        //加载布局到当前组件
        LayoutInflater.from(context).inflate(R.layout.layout_tab_normal,this,true)
        //获取组件
        mIcon = findViewById(R.id.tab_icon)
        mTitle = findViewById(R.id.tab_title)
        mMsgView = findViewById(R.id.tab_msg)


    }

    fun initData(@DrawableRes defaultIcon:Int, @DrawableRes checkedIcon:Int, title:String, tabCheck:Boolean = false, msgNum:Int = 0){
        //获取图标
        mDefaultIcon = ContextCompat.getDrawable(context,defaultIcon)!!
        mCheckedIcon = ContextCompat.getDrawable(context,checkedIcon)!!

        mTabTitle = title
        mChecked = tabCheck
        mMsgNum = msgNum

        mTitle.text = mTabTitle

    }


    override fun setChecked(checked: Boolean) {
        mIcon.setImageDrawable(if(checked) mCheckedIcon else mDefaultIcon)
        mTitle.setTextColor(if(checked) Color.parseColor(mCheckedTextColor) else Color.parseColor(mDefaultTextColor))
        mChecked = checked
    }

    override fun setMessageNumber(number: Int) {
        mMsgView.messageNumber = number
        mMsgNum = number
    }

    override fun setHasMessage(hasMessage: Boolean) {
        mMsgView.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String) {
        mTitle.text = title
        mTabTitle = title
    }

    override fun setDefaultDrawable(drawable: Drawable) {
        mDefaultIcon = drawable
        if (!mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable) {
        mCheckedIcon = drawable
        if (mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun getTitle(): String {
        return mTitle.text.toString()
    }
}